package com.springsource.boot.autoConfig;

import org.springframework.context.annotation.*;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.List;

public class A41 {

    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("config", Config.class);
        context.registerBean(ConfigurationClassPostProcessor.class);
        context.refresh();

        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }

    }

    @Configuration // 自己的配置类
//    @Import({AutoConfiguration1.class, AutoConfiguration2.class}) // 导入第三方配置，import写法
    @Import(MyImportSelector.class) // 导入第三方配置，importSelector写法，不是写死注解，而是将要导入的类写入ImportSelector中
    static class Config {

    }

    static class MyImportSelector implements ImportSelector {
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            // 写死的类
//            return new String[]{AutoConfiguration1.class.getName(), AutoConfiguration2.class.getName()};
            // 从spring.factories配置文件读，会找jar包中所有的spring.factories文件
            List<String> strings = SpringFactoriesLoader.loadFactoryNames(MyImportSelector.class, null);
            return strings.toArray(new String[0]);
        }
    }




    @Configuration // 第三方配置类
    static class AutoConfiguration1 {
        @Bean
        public Bean1 bean1() {
            return new Bean1();
        }
    }


    static class Bean1 {}

    @Configuration // 第三方配置类
    static class AutoConfiguration2 {
        @Bean
        public Bean2 bean2() {
            return new Bean2();
        }
    }


    static class Bean2 {}
}
